﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _02_委托练习
{
    public delegate void DelChangePointState(PointState pointState);
    public delegate void DelToolStripMenuItemEable(bool enable);
    public delegate void DelSetButtonLocation(int x, int y);
    //创建委托用于设置步长与速度
    public delegate void DelSetStep(int step);
    public delegate void DelSetSpeed(int speed);

    public partial class MainForm : Form
    {
        //构建枚举状态
        PointState pointState;
        //创建步长
        int step = 1;
        //委托对象初始化
        public static DelChangePointState delChangePointState;
        //产生三个对象，用于提供给子窗体调用，设置菜单项的可用性
        public static DelToolStripMenuItemEable jop运行delToolStripMenuItemEable;
        public static DelToolStripMenuItemEable p2P运动delToolStripMenuItemEable;
        public static DelToolStripMenuItemEable 设置delToolStripMenuItemEable;
        //委托对象=>设置按钮位置
        public static DelSetButtonLocation delSetButtonLocation;
        //委托对象=>设置步长
        public static DelSetStep delSetStep;
        //委托对象=>设置速度
        public static DelSetSpeed delSetSpeed;


        public MainForm()
        {
            InitializeComponent();
            //委托对象实例化
            delChangePointState = new DelChangePointState(ChangePointState);
            //实例化三个委托对象
            jop运行delToolStripMenuItemEable = enable => this.jop运行ToolStripMenuItem.Enabled = enable;
            p2P运动delToolStripMenuItemEable = enable => this.p2P运动ToolStripMenuItem.Enabled = enable;
            设置delToolStripMenuItemEable = enable => this.设置ToolStripMenuItem.Enabled = enable;
            //实例化委托对象=>设置按钮位置
            delSetButtonLocation = new DelSetButtonLocation(SetButtonLocation);
            //实例化委托对象=>设置步长
            delSetStep = value => step = value;
            //实例化委托对象=>设置速度
            delSetSpeed = value => this.timer1.Interval = value;

        }

        private void jop运行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_JopMove frm_JopMove = new Frm_JopMove();
            frm_JopMove.Show();
            this.jop运行ToolStripMenuItem.Enabled = false;
        }

        private void p2P运动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_P2PMove frm_P2PMove = new Frm_P2PMove(this.button1.Location.X,this.button1.Location.Y);
            frm_P2PMove.Show();
            this.p2P运动ToolStripMenuItem.Enabled = false;
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_SetSpeed frm_SetSpeed = new Frm_SetSpeed(step,this.timer1.Interval);
            frm_SetSpeed.Show();
            this.设置ToolStripMenuItem.Enabled = false;
        }



        /// <summary>
        /// 定时器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (pointState)
            {
                case PointState.None:
                    break;
                case PointState.Up:
                    this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y - step);
                    break;
                case PointState.Down:
                    this.button1.Location = new Point(this.button1.Location.X, this.button1.Location.Y + step);
                    break;
                case PointState.Left:
                    this.button1.Location = new Point(this.button1.Location.X - step, this.button1.Location.Y);
                    break;
                case PointState.Right:
                    this.button1.Location = new Point(this.button1.Location.X + step, this.button1.Location.Y);
                    break;
            }
            //显示X坐标
            this.toolStripStatusLabel1.Text = this.button1.Location.X.ToString();
            //显示Y坐标
            this.toolStripStatusLabel4.Text = this.button1.Location.Y.ToString();
            //显示步长
            this.toolStripStatusLabel6.Text = step.ToString();
            //显示时间间隔
            this.toolStripStatusLabel8.Text = this.timer1.Interval.ToString();

        }
        /// <summary>
        /// 改变方向状态
        /// </summary>
        /// <param name="pointState"></param>
        public void ChangePointState(PointState pointState)
        {
            this.pointState = pointState;
        }


        public void SetButtonLocation(int x, int y)
        {
            this.button1.Location = new Point(x, y);
        }

    }
}
